
let p1 = new Promise(res => {
    setTimeout(() => {
        res(1)
    }, 2000)
})

let p2 = new Promise(res => {
    setTimeout(() => {
        res(2)
    }, 1000)
})

// let p3 = 3

let promises = [p1, p2]

Promise.myRace = function (promises) {
    return new Promise((resolve, reject) => {
        for (let i = 0; i < promises.length; i++) {
            let curPromise = promises[i]
            if (curPromise.then && typeof curPromise.then === 'function') {
                curPromise.then((value) => {
                    resolve(value)
                }, (e) => {
                    reject(e)
                })
            } else {
                resolve(curPromise)
            }

        }
    })
}


Promise.race(promises).then(res => {
    console.log(res) // 3
})

Promise.myRace(promises).then(res => {
    console.log(res) // 3
})